| Conditions | 1 |
| Paths | 1 |
| Total Lines | 203 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* jshint -W101, -W098 */ |
||
| 293 | describe('webhooks api', function() { |
||
| 294 | var createdWebhooks = []; |
||
| 295 | var cleanup = function(done) { |
||
| 296 | client.allWebhooks({page:1, limit:500}, function(err, response) { |
||
| 297 | //delete each webhook |
||
| 298 | //var allWebhooks = response.data; |
||
| 299 | if (!createdWebhooks.length) { |
||
| 300 | done(); |
||
| 301 | } |
||
| 302 | createdWebhooks.forEach(function(identifier) { |
||
| 303 | client.deleteWebhook(identifier, function(err, response) { |
||
| 304 | createdWebhooks.splice(identifier, 1); |
||
| 305 | if (createdWebhooks.length === 0) { |
||
| 306 | done(); |
||
| 307 | } |
||
| 308 | }); |
||
| 309 | }); |
||
| 310 | }); |
||
| 311 | }; |
||
| 312 | before(function(done) { |
||
| 313 | // runs before all tests in this block..cleanup any existing data that could conflict with the tests |
||
| 314 | cleanup(done); |
||
| 315 | }); |
||
| 316 | after(function(done) { |
||
| 317 | //cleanup after all tests |
||
| 318 | cleanup(done); |
||
| 319 | }); |
||
| 320 | |||
| 321 | //create a custom (yet "random") identifier such to avoid conflicts when running multiple tests simultaneously |
||
| 322 | var myIdentifier = crypto.randomBytes(24).toString('hex'); |
||
| 323 | |||
| 324 | // test cases |
||
| 325 | it('create new webhook with custom identifier', function(done) { |
||
| 326 | client.setupWebhook("https://www.blocktrail.com/webhook-test", myIdentifier, function(err, webhook) { |
||
| 327 | assert.ifError(err); |
||
| 328 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); |
||
| 329 | assert.equal(webhook.identifier, myIdentifier); |
||
| 330 | createdWebhooks.push(webhook.identifier); |
||
| 331 | done(); |
||
| 332 | }); |
||
| 333 | }); |
||
| 334 | |||
| 335 | it('create new webhook with random identifier', function(done) { |
||
| 336 | client.setupWebhook("https://www.blocktrail.com/webhook-test", function(err, webhook) { |
||
| 337 | assert.ifError(err); |
||
| 338 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); |
||
| 339 | assert.ok(webhook.identifier); |
||
| 340 | createdWebhooks.push(webhook.identifier); |
||
| 341 | done(); |
||
| 342 | }); |
||
| 343 | }); |
||
| 344 | |||
| 345 | it('get all user webhooks', function(done) { |
||
| 346 | client.allWebhooks(null, function(err, response) { |
||
| 347 | assert.ifError(err); |
||
| 348 | assert.ok('data' in response, "'data' key not in response"); |
||
| 349 | assert.ok('total' in response, "'total' key not in response"); |
||
| 350 | assert.ok(parseInt(response['total']) >= 2, "'total' does not match expected value"); |
||
| 351 | assert.ok(response['data'].length >= 2, "Count of webhooks returned is not equal to 2"); |
||
| 352 | |||
| 353 | assert.ok('url' in response['data'][0], "'url' key not in first webhook of response"); |
||
| 354 | assert.ok('url' in response['data'][1], "'url' key not in second webhook of response"); |
||
| 355 | done(); |
||
| 356 | }); |
||
| 357 | }); |
||
| 358 | |||
| 359 | it('get a single webhook', function(done) { |
||
| 360 | client.getWebhook(createdWebhooks[0], function(err, response) { |
||
| 361 | assert.ifError(err); |
||
| 362 | assert.ok('url' in response, "'url' key not in response"); |
||
| 363 | assert.ok('identifier' in response, "'identifier' key not in response"); |
||
| 364 | assert.equal(response['url'], "https://www.blocktrail.com/webhook-test", "'url' does not match expected value"); |
||
| 365 | assert.equal(response['identifier'], myIdentifier, "'identifier' does not match expected value"); |
||
| 366 | done(); |
||
| 367 | }); |
||
| 368 | }); |
||
| 369 | |||
| 370 | it('delete a webhook', function(done) { |
||
| 371 | client.deleteWebhook(createdWebhooks[0], function(err, response) { |
||
| 372 | assert.ifError(err); |
||
| 373 | assert.ok(response); |
||
| 374 | done(); |
||
| 375 | }); |
||
| 376 | }); |
||
| 377 | |||
| 378 | it('update a webhook', function(done) { |
||
| 379 | var newIdentifier = crypto.randomBytes(24).toString('hex'); |
||
| 380 | var newUrl = "https://www.blocktrail.com/new-webhook-url"; |
||
| 381 | client.updateWebhook(createdWebhooks[1], {identifier: newIdentifier, url: newUrl}, function(err, response) { |
||
| 382 | assert.ifError(err); |
||
| 383 | assert.ok('url' in response, "'url' key not in response"); |
||
| 384 | assert.ok('identifier' in response, "'identifier' key not in response"); |
||
| 385 | assert.equal(response['url'], newUrl, "'url' does not match expected value"); |
||
| 386 | assert.equal(response['identifier'], newIdentifier, "'identifier' does not match expected value"); |
||
| 387 | |||
| 388 | createdWebhooks[1] = newIdentifier; |
||
| 389 | done(); |
||
| 390 | }); |
||
| 391 | }); |
||
| 392 | |||
| 393 | it('subscribe to address-transaction events', function(done) { |
||
| 394 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; |
||
| 395 | client.subscribeAddressTransactions(createdWebhooks[1], address, 2, function(err, response) { |
||
| 396 | assert.ifError(err); |
||
| 397 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
| 398 | assert.ok('address' in response, "'address' key not in response"); |
||
| 399 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
| 400 | assert.equal(response['event_type'], "address-transactions", "'event_type' does not match expected value"); |
||
| 401 | assert.equal(response['address'], address, "'address' does not match expected value"); |
||
| 402 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); |
||
| 403 | done(); |
||
| 404 | }); |
||
| 405 | }); |
||
| 406 | |||
| 407 | it('subscribe to transaction event', function(done) { |
||
| 408 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; |
||
| 409 | client.subscribeTransaction(createdWebhooks[1], transaction, 2, function(err, response) { |
||
| 410 | assert.ifError(err); |
||
| 411 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
| 412 | assert.ok('address' in response, "'address' key not in response"); |
||
| 413 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
| 414 | assert.equal(response['event_type'], "transaction", "'event_type' does not match expected value"); |
||
| 415 | assert.equal(response['transaction'], transaction, "'transaction' does not match expected value"); |
||
| 416 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); |
||
| 417 | done(); |
||
| 418 | }); |
||
| 419 | }); |
||
| 420 | |||
| 421 | it('subscribe to new block events', function(done) { |
||
| 422 | client.subscribeNewBlocks(createdWebhooks[1], function(err, response) { |
||
| 423 | assert.ifError(err); |
||
| 424 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
| 425 | assert.ok('address' in response, "'address' key not in response"); |
||
| 426 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
| 427 | assert.equal(response['event_type'], "block", "'event_type' does not match expected value"); |
||
| 428 | assert.equal(response['address'], null, "'address' does not match expected value"); |
||
| 429 | assert.equal(response['confirmations'], null, "'confirmations' does not match expected value"); |
||
| 430 | done(); |
||
| 431 | }); |
||
| 432 | }); |
||
| 433 | |||
| 434 | it('batch subscribe to address-transaction events', function(done) { |
||
| 435 | var batchData = [ |
||
| 436 | { |
||
| 437 | 'event_type': 'address-transactions', |
||
| 438 | 'address': '18FA8Tn54Hu8fjn7kkfAygPoGEJLHMbHzo', |
||
| 439 | 'confirmations': 1 |
||
| 440 | }, |
||
| 441 | { |
||
| 442 | 'address': '1LUCKYwD6V9JHVXAFEEjyQSD4Dj5GLXmte', |
||
| 443 | 'confirmations': 1 |
||
| 444 | }, |
||
| 445 | { |
||
| 446 | 'address': '1qMBuZnrmGoAc2MWyTnSgoLuWReDHNYyF' |
||
| 447 | } |
||
| 448 | ]; |
||
| 449 | client.batchSubscribeAddressTransactions(createdWebhooks[1], batchData, function(err, response) { |
||
| 450 | assert.ifError(err); |
||
| 451 | assert.ok(response); |
||
| 452 | done(); |
||
| 453 | }); |
||
| 454 | }); |
||
| 455 | |||
| 456 | it('get webhook event subscriptions', function(done) { |
||
| 457 | client.getWebhookEvents(createdWebhooks[1], function(err, response) { |
||
| 458 | assert.ifError(err); |
||
| 459 | assert.ok('data' in response, "'data' key not in response"); |
||
| 460 | assert.ok('total' in response, "'total' key not in response"); |
||
| 461 | assert.equal(parseInt(response['total']), 6, "'total' does not match expected value"); |
||
| 462 | assert.equal(response['data'].length, 6, "Count of event subscriptions returned is not equal to 2"); |
||
| 463 | |||
| 464 | assert.ok('event_type' in response['data'][0], "'event_type' key not in first event subscription of response"); |
||
| 465 | |||
| 466 | done(); |
||
| 467 | }); |
||
| 468 | }); |
||
| 469 | |||
| 470 | it('unsubscribe from address-transaction events', function(done) { |
||
| 471 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; |
||
| 472 | client.unsubscribeAddressTransactions(createdWebhooks[1], address, function(err, response) { |
||
| 473 | assert.ifError(err); |
||
| 474 | assert.ok(response); |
||
| 475 | done(); |
||
| 476 | }); |
||
| 477 | }); |
||
| 478 | |||
| 479 | it('unsubscribe from new transaction events', function(done) { |
||
| 480 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; |
||
| 481 | client.unsubscribeTransaction(createdWebhooks[1], transaction, function(err, response) { |
||
| 482 | assert.ifError(err); |
||
| 483 | assert.ok(response); |
||
| 484 | done(); |
||
| 485 | }); |
||
| 486 | }); |
||
| 487 | |||
| 488 | it('unsubscribe from new block events', function(done) { |
||
| 489 | client.unsubscribeNewBlocks(createdWebhooks[1], function(err, response) { |
||
| 490 | assert.ifError(err); |
||
| 491 | assert.ok(response); |
||
| 492 | done(); |
||
| 493 | }); |
||
| 494 | }); |
||
| 495 | }); |
||
| 496 | |||
| 570 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.